
package Views;

import Controllers.WereldController;
import GUI_algemeen.SimulatieGegevensDialog;
import java.awt.Color;

/**
 *
 * @author Bart Janisse
 */
public class KnoppenPaneel extends javax.swing.JPanel {

    WereldController controller;
    /**
     * maak een nieuw knoppen panneel aan.
     * HEt knoppen panneel wordt later als een bean gebruikt om de siumulatie 
     * te bedienen. de bean wordt in de MainViewFrame geplaatst.
     */
    public KnoppenPaneel() {
        initComponents();
        // bij het aanmaken van het beginschem vul het textveld.
        this.jTextField2.setText(this.jSliderSimulationSpeed.getValue()+"");
        this.jTextFieldRunningIndicator.setBackground(Color.blue);
        this.jTextFieldRunningIndicator.setText("simulatie idle");
    }
    
    /**
     * Set de controller die gebruikt dient te worden
     * @param controller 
     */
    public void setController(WereldController controller) {
        this.controller = controller;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnLoadSimulation = new javax.swing.JButton();
        btnSaveSimultation = new javax.swing.JButton();
        btnStartSimulation = new javax.swing.JButton();
        btnNewSimulation = new javax.swing.JButton();
        btnPauzeSimulation = new javax.swing.JButton();
        btnExitSimulation = new javax.swing.JButton();
        jSliderSimulationSpeed = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jTextFieldRunningIndicator = new javax.swing.JTextField();

        btnLoadSimulation.setText("Load simulatie");
        btnLoadSimulation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadSimulationActionPerformed(evt);
            }
        });

        btnSaveSimultation.setText("Save simulatie");
        btnSaveSimultation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveSimultationActionPerformed(evt);
            }
        });

        btnStartSimulation.setText("Start simulatie");
        btnStartSimulation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartSimulationActionPerformed(evt);
            }
        });

        btnNewSimulation.setText("Nieuwe simulatie");
        btnNewSimulation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewSimulationActionPerformed(evt);
            }
        });

        btnPauzeSimulation.setText("Pauze simulatie");
        btnPauzeSimulation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPauzeSimulationActionPerformed(evt);
            }
        });

        btnExitSimulation.setText("Exit ");
        btnExitSimulation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitSimulationActionPerformed(evt);
            }
        });

        jSliderSimulationSpeed.setMaximum(3000);
        jSliderSimulationSpeed.setMinimum(1);
        jSliderSimulationSpeed.setToolTipText("");
        jSliderSimulationSpeed.setValue(1500);
        jSliderSimulationSpeed.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jSliderSimulationSpeed.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderSimulationSpeedStateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel1.setText(" Simulatie snelheid ");
        jLabel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jTextFieldRunningIndicator.setEditable(false);
        jTextFieldRunningIndicator.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jTextFieldRunningIndicator.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextFieldRunningIndicator.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnSaveSimultation, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                    .addComponent(btnLoadSimulation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnPauzeSimulation, javax.swing.GroupLayout.DEFAULT_SIZE, 107, Short.MAX_VALUE)
                    .addComponent(btnStartSimulation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnExitSimulation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnNewSimulation, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSliderSimulationSpeed, javax.swing.GroupLayout.DEFAULT_SIZE, 377, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 59, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextFieldRunningIndicator, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLoadSimulation)
                    .addComponent(btnNewSimulation)
                    .addComponent(btnStartSimulation))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSaveSimultation)
                    .addComponent(btnExitSimulation)
                    .addComponent(btnPauzeSimulation))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldRunningIndicator, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderSimulationSpeed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnLoadSimulationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadSimulationActionPerformed
        /**
         * Met dit even wordt een simulatie uit de database geladen
         */
        controller.cmdLaadSimulatie();
    }//GEN-LAST:event_btnLoadSimulationActionPerformed

    private void btnSaveSimultationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveSimultationActionPerformed
        /**
         * Met dit event wordt een simulatie opgelagen in de database
         */
        controller.cmdSaveSimulatie();
    }//GEN-LAST:event_btnSaveSimultationActionPerformed

    private void btnExitSimulationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitSimulationActionPerformed
        /**
         * Met dit even wordt de applicatie afgesloten
         */
        controller.cmdExit();
    }//GEN-LAST:event_btnExitSimulationActionPerformed

    private void btnStartSimulationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartSimulationActionPerformed
        /**
         * Met dit event wordt de simulatie gestart en als de simulatie
         * gestart is wordt de indicator en de text aangepast in het scherm
         */
        controller.cmdStartSimulatie();
        if (controller.getSimulatieLoopt())
        {
           this.jTextFieldRunningIndicator.setBackground(Color.green);
           this.jTextFieldRunningIndicator.setText("simulatie running");
        }
    }//GEN-LAST:event_btnStartSimulationActionPerformed

    private void btnPauzeSimulationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPauzeSimulationActionPerformed
        /**
         * Met dit event wordt de simulatie gepauzeerd en als de simulatie
         * gepauzeerd is wordt de indicator en de text aangepast in het scherm
         */
        controller.cmdPauzeSimulatie();
        if (!controller.getSimulatieLoopt())
        {
           this.jTextFieldRunningIndicator.setBackground(Color.red);
           this.jTextFieldRunningIndicator.setText("simulatie gepauzeerd");
        }
    }//GEN-LAST:event_btnPauzeSimulationActionPerformed

    private void btnNewSimulationActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewSimulationActionPerformed
        /**
         * Met dit event wordt een nieuwe simulatie aangemaakt. 
         * Er wordt een dialoog geopend om de instellingen evt te wijzigen.
         * Als er op oke in het dialoog gedrukt wordt, zal de simulatie
         * gevreerd worden met de ingevoede gegevens. De gegeven komen uit de
         * invoer velden of uit de XML file.
         */
        SimulatieGegevensDialog simulatieGegevens = new SimulatieGegevensDialog(null, true);
        simulatieGegevens.setTitle("Simulatie Gegevens");
        simulatieGegevens.setVisible(true);
        
        // Nieuwe simulatie wordt alleen aangemaakt indien er op de oke knop gedrukt wordt in de invoer dialog
        if (simulatieGegevens.getReturnStatus() == 1 )
        {
            controller.cmdNieuweSimulatie();
        }
    }//GEN-LAST:event_btnNewSimulationActionPerformed

    private void jSliderSimulationSpeedStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderSimulationSpeedStateChanged

        /**
         * Indien de slider in het scher verplaatst wordt zal de snelheid van
         * de simulatie worden aan gepast.
         */
       int speed = jSliderSimulationSpeed.getValue();
       controller.cmdSetSnelheid(speed);
       this.jTextField2.setText(""+speed);
    }//GEN-LAST:event_jSliderSimulationSpeedStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExitSimulation;
    private javax.swing.JButton btnLoadSimulation;
    private javax.swing.JButton btnNewSimulation;
    private javax.swing.JButton btnPauzeSimulation;
    private javax.swing.JButton btnSaveSimultation;
    private javax.swing.JButton btnStartSimulation;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSlider jSliderSimulationSpeed;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextFieldRunningIndicator;
    // End of variables declaration//GEN-END:variables
}
